<template>
  <div class="app-container">
    <el-form class="my_search_form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="问题名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item class="btns">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="my_main">
      <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['article:faq:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['article:faq:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['article:faq:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['article:faq:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="categoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分类名称" align="center" prop="name" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleQA(scope.row)" v-hasPermi="['article:faq:edit']">问答</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['article:faq:edit']">修改</el-button>
          <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['article:faq:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    </div>

    <!-- 添加或修改文章分类对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form class="my_form" ref="categoryRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问题名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog :title="title" v-model="open2" width="1100px" append-to-body>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="Plus"
              @click="handleAddQ"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDeleteQ"
            >删除</el-button>
          </el-col>
      </el-row>
      <el-table v-loading="loading" :data="articleList">
        <el-table-column label="问题" align="center" prop="name" />
        <el-table-column label="回答" align="center" prop="content" :show-overflow-tooltip="true"/>
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdateQ(scope.row)">修改</el-button>
            <el-button link type="danger" icon="Delete" @click="handleDeleteQ(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
     <!-- 添加或修改文章内容对话框 -->
     <el-dialog :title="title2" v-model="open3" :close-on-click-modal="false" width="720px" append-to-body>
      <el-form ref="articleRef" :model="form2" :rules="rules" label-width="100px">
        <el-form-item label="问题" prop="name">
          <el-input v-model="form2.name" placeholder="请输入问题" />
        </el-form-item>
        <el-form-item label="回答" prop="content">
          <el-input v-model="form2.content" type="textarea" placeholder="请输入回答" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm2">确 定</el-button>
          <el-button @click="cancel2">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Category">
import { listCategory, getCategory, delCategory, addCategory, updateCategory } from "@/api/article/category";
import { listArticle, getArticle, delArticle, addArticle, updateArticle } from "@/api/article/article";
const { proxy } = getCurrentInstance();
const articleRef = ref()
const categoryList = ref([]);
const open = ref(false);
const open2 = ref(false)
const open3 = ref(false)
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const ids2 = ref([])
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const title2 = ref("");
const articleList = ref([])

const data = reactive({
  form: {},
  form2: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null
  },
  rules: {
    name: [
      { required: true, message: "分类名称不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules, form2 } = toRefs(data);

/** 查询文章分类列表 */
function getList() {
  loading.value = true;
  queryParams.value.type = "FAQ"
  listCategory(queryParams.value).then(response => {
    categoryList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null
  };
  proxy.resetForm("categoryRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加分类";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getCategory(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改分类";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["categoryRef"].validate(valid => {
    if (valid) {
      form.value.type = "FAQ"
      if (form.value.id != null) {
        updateCategory(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCategory(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除文章分类编号为"' + _ids + '"的数据项？').then(function() {
    return delCategory(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('article/category/export', {
    ...queryParams.value
  }, `category_${new Date().getTime()}.xlsx`)
}
function handleQA(row) {
  const _id = row.id
  loading.value = true;
  getCategory(_id).then(response => {
    form.value = response.data;
    open2.value = true
    title.value = form.value.name
    loading.value = false;
    getList2()
  });
}
function getList2() {
  const _id = form.value.id
  loading.value = true;
  listArticle({ categoryId: _id, pageSize: 10000 }).then(res => {
    articleList.value = res.rows;
    loading.value = false;
  })
}
function handleAddQ() {
  title2.value = '新增疑答'
  open3.value = true
  form2.value = {}
}
function handleUpdateQ(row) {
  title2.value = '修改疑答'
  open3.value = true
  getArticle(row.id).then(res => {
    form2.value = res.data
  })
}
function handleDeleteQ(row) {
  const _ids = row.id || ids2.value;
  proxy.$modal.confirm('是否确认删除文章分类编号为"' + _ids + '"的数据项？').then(function() {
    return delArticle(_ids);
  }).then(() => {
    getList2();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}


function cancel2() {
  open3.value = false
  form2.value = {}
}
function submitForm2() {
  proxy.$refs["articleRef"].validate(valid => {
    if (valid) {
      form2.value.type = "FAQ"
      form2.value.categoryId = form.value.id
      form2.publish = true
      if (form.value.id != null) {
        updateArticle(form2.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open3.value = false;
          getList2();
        });
      } else {
        addArticle(form2.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open3.value = false;
          getList2();
        });
      }
    }
  });
}
getList();
</script>
